﻿using AutoMapper;
using FeishuWorkflowService.Models;
using System.Globalization;

namespace FeishuWorkflowService.Mappings
{
    public class MapperProfile : Profile
    {
        public MapperProfile()
        {
            // 1. 基础映射：Widget → FormItem
            CreateMap<Widget, FormItem?>()
                .ForMember(dest => dest.id, opt => opt.MapFrom(src => src.custom_id))
                .ForMember(dest => dest.type, opt => opt.MapFrom(src => src.type))
                .ForMember(dest => dest.value, opt => opt.MapFrom(src => src.widget_default_value));

            // 2. 映射到元组：Widget → (FormItem? form, List<FormItem>? children)
            CreateMap<Widget, (FormItem? form, List<FormItem>? children)>()
                .ForPath(dest => dest.form, opt => opt.MapFrom(src => src)) // 复用基础映射
                .ForMember(dest => dest.children, opt => opt.MapFrom((src, _, _, context) =>
                    src.children?.Select(child => context.Mapper.Map<FormItem>(child)).ToList()
                ));

            // 3. 数组转列表：Widget[] → List<(FormItem? form, List<FormItem>? children)>
            CreateMap<Widget[], List<(FormItem? form, List<FormItem>? children)>>()
                .ConvertUsing((src, _, context) =>
                    src?.Select(widget => context.Mapper.Map<(FormItem? form, List<FormItem>? children)>(widget))
                        .ToList()
                );

            // 4. 列表转列表：List<Widget> → List<(FormItem? form, List<FormItem>? children)>
            CreateMap<List<Widget>, List<(FormItem? form, List<FormItem>? children)>>()
                .ConvertUsing((src, _, context) =>
                    src?.Select(widget => context.Mapper.Map<(FormItem? form, List<FormItem>? children)>(widget))
                        .ToList()
                );
            
        }
    }
}
